% Latex style for evaltrace diagrams in 11pt documents.
% Copyright, Peter Lee and David Touretzky, 1989
%
% See "Visualizing evaluation in applicative languages",
%  by David S. Touretzky and Peter Lee
%
% This style is loosely based on Jon Rees' code.sty, which was
% originally based on macros found in Chuck Rich's library.

\makeatletter

\font\et et10
\font\cmtt cmtt10

% \etobeyspaces turns all spaces into non-breakable spaces.
% Note: this is like \@vobeyspaces except without spurious space in defn.
{\catcode`\ =\active\gdef\etobeyspaces{\catcode`\ =\active\let =\ttxobeysp}}
\def\ttxobeysp{{\cmtt\@xobeysp}}  % use the computer modern \tt space.

% \etobeytabs turns all tabs into 8 non-breakable spaces.
{\catcode`\^^I=\active\gdef\etobeytabs{\catcode`\^^I=\active\let^^I=\xetobeytabs}}
\def\xetobeytabs{\@xobeysp\@xobeysp\@xobeysp\@xobeysp\@xobeysp\@xobeysp\@xobeysp\@xobeysp}

% \etobeylines turns all cr's into non-breakable \par's.
{\catcode`\^^M=\active\gdef\etobeylines{\catcode`\^^M=\active\let^^M=\xetobeylines}}
\def\xetobeylines{\par\penalty10000}

% \obeycrsp turns cr's into non-breakable spaces
{\catcode`\^^M=\active\gdef\obeycrsp{\catcode`\^^M=\active\let^^M=\@xobeysp}}

% \specialsemi makes ';text<cr>' behave like '{\tt text}<cr>'.
{\catcode`;=\active\gdef\specialsemi{\catcode`;=\active\let;=\xspecialsemi}}
{\catcode`\^^M=\active\gdef\xspecialsemi#1^^M{{\cmtt #1}^^M}}

% \specialquote makes '"text"' behave like '\verb| text |'.
{\catcode`"=\active\gdef\specialquote{\catcode`"=\active\let"=\xspecialquote}}
{\catcode`"=\active\gdef\xspecialquote#1"{{\cmtt ~#1~}}}

% Set up evaltrace environment, in which most of the common special
% characters appearing in code are treated verbatim, namely:
%                    _ # & ^ $ ~ @ " %
%
% Note: \ { } are still enabled so that macros can be called in this
% environment.  Also, + is an escape character just like \.  Use \\, \+,
% \{ and \} to use these characters verbatim in this environment.
%
% Note: this environment allows no breaking of lines whatsoever; not
% at spaces or hypens.  To arrange for a break use the standard \- macro,
% or the \= macro which breaks, but inserts nothing.  This is useful,
% for example for allowing hypenated identifiers to be broken, e.g.
% FOO-\=BAR.

\def\setupevaltrace{\parsep=0pt\parindent=0pt%
  \small\et\frenchspacing%
  \def\\{\char`\\}\def\+{\char`\+}%
  \catcode`\#=11\catcode`\&=11\catcode`\^=11\catcode`\_=11\catcode`\>=11%
  \catcode`\-=11\catcode`\.=11\catcode`\*=11\catcode`\]=11%
  \catcode`\$=11\catcode`\'=11\catcode`\~=11\catcode`\@=11%
  \catcode`\"=11\catcode`+=0\catcode`\%=11\catcode`\`=11\catcode`\==11%
  \etobeytabs\etobeyspaces\specialsemi\specialquote}

% "Big" evaltrace environment as described above.  Blank lines are
% not preserved (use \strut for this), and diagrams are not kept on
% one page.  Diagrams are indented by the same amount as quotes.
% Note: to increase left margin, use \leftmargini=1in.

\newenvironment{bigevaltrace}%
  {\list{}{\parsep=0pt\leftmargin=0pt\labelwidth=0pt\itemindent=0pt%
\listparindent=0pt}\item[]\setupevaltrace%
\obeylines}%
  {\endlist}

% Evaltrace is just like bigevaltrace, but everything the diagram is
% kept on one page.  This works by setting a huge penalty for breaking
% between lines of the diagram.

\newenvironment{evaltrace}%
  {\list{}{\parsep=0pt\labelwidth=0pt\itemindent=0pt%\leftmargin=0pt
\listparindent=0pt}\item[]\setupevaltrace%
\etobeylines}%
  {\endlist}

% Various macros for drawing joints and arrows.

% \--> and \_-> draw upper and lower thin right arrows.
{\catcode`\>=11\catcode`\-=11\gdef\-->{q->}}
{\catcode`\>=11\catcode`\.=11\catcode`\-=11\gdef\.->{I->}}
{\catcode`\>=11\catcode`\-=11\catcode`\_=11\gdef\_->{a->}}

% \**> and \_*> draw upper and lower thick right arrows.
{\catcode`\>=11\catcode`\*=11\gdef\**>{T\symbol{95}]}}
{\catcode`\>=11\catcode`\_=11\gdef\__>{Q\symbol{95}]}}
{\catcode`\>=11\catcode`\*=11\catcode`\_=11\gdef\_*>{A\symbol{95}]}}

% \%%> and \_%> draw upper and lower hollow right arrows.
{\catcode`\>=11\catcode`\%=11\gdef\%%>{t=!}}
{\catcode`\>=11\catcode`\%=11\catcode`\_=11\gdef\_%>{D=!}}
{\catcode`\>=11\catcode`\==11\gdef\==>{E=!}}

% \..> and \_.> draw upper and lower dashed right arrows.
{\catcode`\>=11\catcode`\.=11\gdef\..>{e.)}}
{\catcode`\>=11\catcode`\.=11\catcode`\_=11\gdef\_.>{d.)}}

\makeatother
